package com.ovo.rxandroiddemo.network;

import com.ovo.network.utils.IdeaApi;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

/**
 * 服务器地址配置
 */
public class RetrofitHelper {
    private static ApiService apiService;
    //正式
//    public static String SERVER = "http://wlc.renliguanli.com/";
//    public static String SERVERS = "http://wlc.renliguanli.com";
    //测试
    public static String SERVER = "http://192.168.1.207:8080/";
    public static String SERVERS = "http://192.168.1.207:8080";

    static {
        apiService = IdeaApi.getServerApi(ApiService.class, SERVER);
    }

    public static ApiService getApiServer() {
        return apiService;
    }

    /**
     * session回话过期重新登录
     * @param toBeResumed
     * @param <T>
     * @return
     */
    public static <T> Function<Throwable, ? extends Observable<? extends T>> refreshTokenAndRetry(final Observable<T> toBeResumed, HashMap<String, Object> reqMap) {
        return new Function<Throwable, Observable<? extends T>>() {
            @Override
            public Observable<? extends T> apply(Throwable t) {
                /*if ("403".equals(t.getMessage())){
                    String account = (String) SPUtils.get(SPUtils.account, "");
                    String pwd = (String) SPUtils.get(SPUtils.pwd, "");
                    String wxUnionId = (String) SPUtils.get(SPUtils.wxUnionId, "");
                    Observable observable = null;
                    if (!StringUtils.isEmpty(account) && !StringUtils.isEmpty(pwd)){
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("loginCode", account);
                        map.put("passWord", AESOperatorUtils.encrypt(pwd));
                        observable = RetrofitHelper.getApiServer().loginPwd(map);
                    }else  if (!StringUtils.isEmpty(wxUnionId)){
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("unionId", wxUnionId);
                        observable = RetrofitHelper.getApiServer().loginWeChat(map);
                    }else {
                        IntentUtils.GoActivity(LoginActivity.class);
                        return Observable.error(t);
                    }
                    if (observable == null){
                        IntentUtils.GoActivity(LoginActivity.class);
                        return Observable.error(t);
                    }
                    return (Observable<? extends T>) observable.subscribeOn(Schedulers.newThread())
                            .observeOn(Schedulers.newThread())
                            .flatMap(new Function<WeChatLoginInfo, ObservableSource<? extends R>>() {
                                @Override
                                public ObservableSource<? extends R> apply(WeChatLoginInfo response) {
                                    reqMap.put("__sid", response.getSid());
                                    return (ObservableSource<? extends R>) toBeResumed;
                                }
                            });
                }*/
                return Observable.error(t);
            }

        };
    }
}
